package com.kzj.laimeimei.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.PaySuccessActivity;
import com.kzj.laimeimei.adapter.OrderFragmentAdapter;
import com.kzj.laimeimei.beans.CloseActivityBean;
import com.kzj.laimeimei.beans.OrderBean;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/10/9.
 */
public class OrderFragment extends BaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private LinearLayout linearlayout_head;
    private TextView textView_all_order;
    private TextView textView_pay;
    private TextView textView_consumption;
    private TextView textView_evaluation;
    private View textView_tab_line;
    private RelativeLayout relativeLayout_all_order;
    private RelativeLayout relativeLayout_evaluation;
    private RelativeLayout relativeLayout_consumption;
    private RelativeLayout relativeLayout_pay;
    private android.support.v4.view.ViewPager viewPager_viewpager;
    private ArrayList<Fragment> fragmentList;
    private OrderFragmentAdapter orderFragmentAdapter;
    private View view1, view2, view3, view4;
    private boolean isScaled;
    private View holderView;
    public static final String PayFlag = "OrderFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.fragment_order, container, false);
            findViewByIds();
            initViews();
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);

        }
        textView_tab_line.setTag(AbViewUtil.NotScale);
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }
        return holderView;
    }

    @Override
    public void findViewByIds() {
        linearlayout_head = (LinearLayout) holderView.findViewById(R.id.Linearlayout_head);
        textView_all_order = (TextView) holderView.findViewById(R.id.textView_all_order);
        textView_pay = (TextView) holderView.findViewById(R.id.textView_pay);
        textView_consumption = (TextView) holderView.findViewById(R.id.textView_consumption);
        textView_evaluation = (TextView) holderView.findViewById(R.id.textView_evaluation);
        textView_tab_line = (View) holderView.findViewById(R.id.textView_tab_line);
        viewPager_viewpager = (android.support.v4.view.ViewPager) holderView.findViewById(R.id.viewPager_viewpager);
        relativeLayout_all_order = (RelativeLayout) holderView.findViewById(R.id.relativeLayout_all_order);
        relativeLayout_evaluation = (RelativeLayout) holderView.findViewById(R.id.relativeLayout_evaluation);
        relativeLayout_consumption = (RelativeLayout) holderView.findViewById(R.id.relativeLayout_consumption);
        relativeLayout_pay = (RelativeLayout) holderView.findViewById(R.id.relativeLayout_pay);
        view1 = holderView.findViewById(R.id.view1);
        view2 = holderView.findViewById(R.id.view2);
        view3 = holderView.findViewById(R.id.view3);
        view4 = holderView.findViewById(R.id.view4);
        viewPager_viewpager.setOffscreenPageLimit(4);
    }

    @Override
    public void initViews() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new AllOrderFragment());
        fragmentList.add(new PayOrderFragment());
        fragmentList.add(new ConsumptionOrderFragment());
        fragmentList.add(new EvaluationOrderFragment());
        orderFragmentAdapter = new OrderFragmentAdapter(getChildFragmentManager(), fragmentList);
        viewPager_viewpager.setAdapter(orderFragmentAdapter);
        viewPager_viewpager.setOffscreenPageLimit(0);
        viewPager_viewpager.setOnPageChangeListener(this);
        relativeLayout_all_order.setOnClickListener(this);
        relativeLayout_consumption.setOnClickListener(this);
        relativeLayout_evaluation.setOnClickListener(this);
        relativeLayout_pay.setOnClickListener(this);
        fragmentList.get(0).setUserVisibleHint(false);
        fragmentList.get(1).setUserVisibleHint(false);
        fragmentList.get(2).setUserVisibleHint(false);
        fragmentList.get(3).setUserVisibleHint(false);
        EventBus.getDefault().register(this);
    }

    @Override
    public void fillArrayInList() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        changeState();
        switch (position) {
            case 0:
                textView_all_order.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view1.setVisibility(View.VISIBLE);
                ((AllOrderFragment) fragmentList.get(0)).refresh();
                break;
            case 1:
                textView_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view2.setVisibility(View.VISIBLE);
                ((PayOrderFragment) fragmentList.get(1)).refresh();
                break;
            case 2:
                textView_consumption.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view3.setVisibility(View.VISIBLE);
                ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                break;
            case 3:
                textView_evaluation.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view4.setVisibility(View.VISIBLE);
                ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void changeState() {
        textView_all_order.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_consumption.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_evaluation.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_pay.setTextColor(getResources().getColor(R.color.color_808080_gray));
        view1.setVisibility(View.GONE);
        view2.setVisibility(View.GONE);
        view3.setVisibility(View.GONE);
        view4.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        changeState();
        switch (v.getId()) {
            case R.id.relativeLayout_all_order:
                textView_all_order.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view1.setVisibility(View.VISIBLE);
                viewPager_viewpager.setCurrentItem(0, false);
                ((AllOrderFragment) fragmentList.get(0)).refresh();
                break;
            case R.id.relativeLayout_consumption:
                textView_consumption.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view3.setVisibility(View.VISIBLE);
                viewPager_viewpager.setCurrentItem(2, false);
                ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                break;
            case R.id.relativeLayout_pay:
                textView_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view2.setVisibility(View.VISIBLE);
                viewPager_viewpager.setCurrentItem(1, false);
                ((PayOrderFragment) fragmentList.get(1)).refresh();
                break;
            case R.id.relativeLayout_evaluation:
                textView_evaluation.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view4.setVisibility(View.VISIBLE);
                viewPager_viewpager.setCurrentItem(3, false);
                ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                break;
        }
    }

    public void onEventMainThread(OrderBean orderBean) {
        if (fragmentList != null) {
            for (int i = 0; i < fragmentList.size(); i++) {
                if (fragmentList.get(i).getUserVisibleHint()) {
                    if (i == 0) {
                        ((AllOrderFragment) fragmentList.get(0)).refresh();
                    }
                    if (i == 1) {
                        ((PayOrderFragment) fragmentList.get(1)).refresh();
                    }
                    if (i == 2) {
                        ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                    }
                    if (i == 3) {
                        ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                    }
                }
            }
        }
    }

    public void onEventMainThread(CloseActivityBean closeActivityBean) {
        if (fragmentList != null) {
            for (int i = 0; i < fragmentList.size(); i++) {
                if (fragmentList.get(i).getUserVisibleHint()) {
                    if (i == 0) {
                        ((AllOrderFragment) fragmentList.get(0)).refresh();
                    }
                    if (i == 1) {
                        ((PayOrderFragment) fragmentList.get(1)).refresh();
                    }
                    if (i == 2) {
                        ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                    }
                    if (i == 3) {
                        ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                    }
                }
            }
        }
        if (closeActivityBean.getOrderBean() != null) {
            if (closeActivityBean.isSuccess()) {
                if (closeActivityBean.getFromActivity().equals(PayFlag)) {
                    Intent intent = new Intent(getActivity(), PaySuccessActivity.class);
                    intent.putExtra("order_bean", closeActivityBean.getOrderBean());
                    startActivity(intent);
                }
            } else {

            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (fragmentList != null) {
            if (isVisibleToUser) {
                for (int i = 0; i < fragmentList.size(); i++) {
                    if (fragmentList.get(i).getUserVisibleHint()) {
                        if (i == 0) {
                            ((AllOrderFragment) fragmentList.get(0)).refresh();
                        }
                        if (i == 1) {
                            ((PayOrderFragment) fragmentList.get(1)).refresh();
                        }
                        if (i == 2) {
                            ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                        }
                        if (i == 3) {
                            ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                        }
                    }
                }
            } else {
                for (int i = 0; i < fragmentList.size(); i++) {
                    if (fragmentList.get(i).getUserVisibleHint()) {
                        if (i == 0) {
                            ((AllOrderFragment) fragmentList.get(0)).clear();
                        }
                        if (i == 1) {
                            ((PayOrderFragment) fragmentList.get(1)).clear();
                        }
                        if (i == 2) {
                            ((ConsumptionOrderFragment) fragmentList.get(2)).clear();
                        }
                        if (i == 3) {
                            ((EvaluationOrderFragment) fragmentList.get(3)).clear();
                        }
                    }
                }
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainActivity"); //统计页面
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainActivity");
    }
}
